package com.Nahida.NaHome.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.Nahida.NaHome.R;
import com.Nahida.NaHome.listener.OnItemClickListener;
import com.Nahida.NaHome.model.HomeButton;
import com.Nahida.NaHome.util.MsgUtil;
import com.google.android.material.card.MaterialCardView;

import java.util.ArrayList;

//公告
//2024-02-23

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.Holder> {

    final Context context;
    final ArrayList<HomeButton> list;
    OnItemClickListener clickListener, longClickListener;

    public HomeAdapter(Context context, ArrayList<HomeButton> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnClickListener(OnItemClickListener l) {
        this.clickListener = l;
    }

    public void setOnLongClickListener(OnItemClickListener l) {
    	this.longClickListener = l;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this.context).inflate(R.layout.cell_button_home, parent, false);
        return new Holder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        HomeButton button = list.get(position);

        holder.button_image.setImageResource(button.icon_id);
        holder.button.setOnClickListener(view -> {
            if(clickListener!=null) clickListener.onClick(position);    //先让监听器响应，因为要设置flag_gotojxw
            if(button.packageName!=null && !button.packageName.isEmpty()) {
                try {
                    Intent intent;
                    if(button.activityName!=null && !button.activityName.isEmpty()) {
                        intent = new Intent();
                        intent.setClassName(button.packageName, button.activityName);
                    }
                    else {
                        intent = context.getPackageManager().getLaunchIntentForPackage(button.packageName);
                    }
                    context.startActivity(intent);
                } catch (Exception e){
                    MsgUtil.showMsg("啊哦，找不到这个页面……\n你确定包名填写正确嘛？", context);
                }
            }
        });
        holder.button.setOnLongClickListener(view->{
            if(longClickListener!=null) longClickListener.onClick(position);
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        MaterialCardView button;
        ImageView button_image;

        public Holder(@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button);
            button_image = itemView.findViewById(R.id.button_image);
        }
    }


}
